/**
 * Copyright 2009-2011 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.myjung.manager;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;

import net.myjung.Myjung;
import net.myjung.exception.MyException;
import net.myjung.model.Mail;
import net.myjung.model.MailAddress;
import net.myjung.model.MailContent;

/**
 * @author Steve M. Jung
 */
public abstract class AbstractMyManager implements IMyManager {
	public void sendMail(Mail mail, Map<String, Object> options) throws MyException {
		// validation
		MailAddress fromAddr = mail.getFrom();
		List<MailAddress> toAddrs = mail.getTos();
		if (fromAddr == null)
			throw new MyException("fromAddressRequired", "From address is required");
		if (Myjung.isEmpty(toAddrs))
			throw new MyException("toAddressRequired", "To address is required");
		List<MailAddress> ccAddrs = mail.getCcs();

		InternetAddress from = null;
		List<InternetAddress> tos = null;
		List<InternetAddress> ccs = null;
		try {
			from = new InternetAddress(fromAddr.getAddress(), fromAddr.getPersonal(), "UTF-8");
			tos = new ArrayList<InternetAddress>();
			for (MailAddress to : toAddrs)
				tos.add(new InternetAddress(to.getAddress(), to.getPersonal(), "UTF-8"));
			if (!Myjung.isEmpty(ccAddrs)) {
				ccs = new ArrayList<InternetAddress>();
				for (MailAddress cc : ccAddrs)
					ccs.add(new InternetAddress(cc.getAddress(), cc.getPersonal(), "UTF-8"));
			}
		} catch (UnsupportedEncodingException e) {
			throw new MyException("invalidMailAddress", "Invalid mail address.", e);
		}

		String subject = mail.getSubject();

		MimeBodyPart content = null;
		MailContent mailContent = mail.getContent();
		if (mailContent != null) {
			content = new MimeBodyPart();
			try {
				content.setContent(mailContent.getText(), mailContent.getType());
			} catch (MessagingException e) {
				throw new MyException("invalidMailContent", "Invalid mail content.", e);
			}
		}

		List<MimeBodyPart> attachments = null;
		// TODO
		//		List<MailAttachment> mailAttachments = mail.getAttachments();
		//		if (!Myjung.isEmpty(mailAttachments)) {
		//			attachments = new ArrayList<MimeBodyPart>();
		//			for (MailAttachment mailAttachment : mailAttachments) {
		//			}
		//		}

		try {
			sendMail(from, tos, ccs, subject, content, attachments);
		} catch (Exception e) {
			throw new MyException("sendMailFailed", "Send mail Failed.", e);
		}
	}

	abstract protected void sendMail(InternetAddress from, List<InternetAddress> tos, List<InternetAddress> ccs, String subject,
			MimeBodyPart content, List<MimeBodyPart> attachments) throws Exception;
}
